//
//  JStoreViewController.m
//  HeadIndustry
//
//  Created by jun on 2017/8/24.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JStoreViewController.h"
#import "UICollectionView+JCollectionView.h"
#import "JProductDetailController.h"
#import "JSearchProductController.h" // 搜索页面
#import "JScendShopControllerViewController.h" // 商品类型二级页面
#import "JYouLickView.h"
#import "JMsgController.h"
@interface JStoreViewController ()<UISearchBarDelegate >

@property (nonatomic , strong) UIScrollView *scrollView;
@property (nonatomic , strong) JYouLickView *collectionView;

@property (nonatomic , strong) NSMutableArray *productArr;

@property (nonatomic , strong) JGetPageModel *pageModel;

@end

@implementation JStoreViewController



- (JGetPageModel *)pageModel
{
    if (!_pageModel) {
        _pageModel = [JGetPageModel new];
        _pageModel.orderBy = 1;
        _pageModel.pageIndex = 1;
        _pageModel.pageSize = 10;
        
    }
    return _pageModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [super InitUI];
    
    self.titleLabel.hidden = YES;
    self.leftButton.hidden = NO;
    self.rightButton.hidden = NO;
    
    // 设置导航搜索栏
    [self setUpNav];

    _productArr = [NSMutableArray array];

    // 配置瀑布流
    [self setUpCollectionView];
    
    // 加载产品列表
//    [self loadProductListArr];
    
    
}

// 配置导航栏
- (void)setUpNav
{
    NSString *userCity;
    if ([UserDefaultsDelegate objectForKey:UserLocationCity] != nil) {
        NSDictionary *cityDic = [UserDefaultsDelegate objectForKey:UserLocationCity];
        userCity = cityDic[@"cityName"];
    }
    else
    {
        userCity = @"武汉市";
    }
    
    [self.leftButton setTitle:userCity forState:UIControlStateNormal];
    [self.rightButton setImage:[UIImage imageNamed:msgBtnImg] forState:UIControlStateNormal];
    
    CGRect searchRect = CGRectMake(CGRectGetMaxX(self.leftButton.frame), 25, CGRectGetMinX(self.rightButton.frame) - CGRectGetMaxX(self.leftButton.frame), 32);

    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:searchRect];
    searchBar.delegate = self;
    searchBar.backgroundColor = [UIColor clearColor];
    
    searchBar.placeholder = @"宝贝搜索";
//    searchBar.showsSearchResultsButton = YES;
    for (UIView *subViews in searchBar.subviews) {
        if ([subViews isKindOfClass:[UIView class]]) {
            [[subViews.subviews objectAtIndex:0] removeFromSuperview];
            
            if ([[subViews.subviews objectAtIndex:0] isKindOfClass:[UITextField class]]) {
                UITextField *textF = [subViews.subviews objectAtIndex:0];
                textF.font = kFONT_TITLE(12);
                textF.backgroundColor = kRGBColor(240, 240, 240);
                textF.returnKeyType = UIReturnKeySearch;
                
                UIColor *color = [UIColor grayColor];
                NSAttributedString *attri = [[NSAttributedString alloc]initWithString:@"宝贝搜索" attributes:@{NSForegroundColorAttributeName:color}];
                
                [textF setAttributedPlaceholder:attri];
            }
        }
    }
//    [searchBar setShowsCancelButton:YES];
    [self.navigationView addSubview:searchBar];

}

// 设置content   瀑布流
- (void)setUpCollectionView
{
    _collectionView = [[JYouLickView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - 49 - 64)];
    _collectionView.titleStr = @"热销产品";
    _collectionView.pageModel = self.pageModel;
    [self.contentView addSubview:_collectionView];
    
    __weak typeof(self) weakSelf = self;
    
//    _collectionView.refreshBlock = ^{
//        weakSelf.pageModel.pageIndex = 0;
//        [weakSelf.productArr removeAllObjects];
//        [weakSelf loadProductListArr];
//    };
    
    _collectionView.refreshCellBlock = ^(NSInteger currentPage) {
        
        weakSelf.pageModel.pageIndex = currentPage;
        weakSelf.pageModel.pageIndex += 1;
        [weakSelf loadProductListArr];
    };
    
    _collectionView.cellSelectBlock = ^(JProductDetailModel *selModel) {
        [weakSelf.view endEditing:YES];

        JProductDetailController *detail = [[JProductDetailController alloc]init];
        detail.productModel = selModel;
        detail.inputType = 1; // 表示非购物车进入商品详情
        detail.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };

    _collectionView.selHeadTypeBlock = ^(JProductTypeModel *model) {
        [weakSelf.view endEditing:YES];
        JScendShopControllerViewController *detail = [[JScendShopControllerViewController alloc]init];
        detail.typeModel = model;
        detail.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };
    
    _collectionView.selHeadkillBlock = ^(JKillProducModel *model) {
        JProductDetailModel *proModel = [JProductDetailModel new];
        proModel.Id = model.ProductId;
        JProductDetailController *detail = [[JProductDetailController alloc]init];
        detail.productModel = proModel;
        detail.inputType = 1; // 表示非购物车进入商品详情
        detail.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };
    
}

- (void)loadProductListArr
{
    
    [SVProgressHUD show];
    NSString *url = @"Products/GetProducts";
    [JNetManager LoadProductListUrl:url pageModel:self.pageModel success:^(id response) {
        if (response) {
            NSMutableArray *dataArr = (NSMutableArray *)response;
            [_productArr addObjectsFromArray:dataArr];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                _collectionView.dataArr = _productArr;

            });
        }
    } failure:^(NSString *err) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"加载失败"];
    }];
}

#pragma mark - buttonClick
// 地区按钮
- (void)leftButtonClick:(UIButton *)leftButton
{
    NSLog(@"地区按钮");
    [self.view endEditing:YES];
}

// 消息按钮
- (void)rightButtonClick:(UIButton *)rightButton
{
    NSLog(@"消息按钮");
    [self.view endEditing:YES];
    
    JMsgController *msgVC = [[JMsgController alloc]init];
    msgVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:msgVC animated:YES];
}

#pragma mark - searchBar
// searchBar
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"===%@", searchBar.text);
    // 调用filterBySubstring:方法执行搜索
//    [self filterBySubstring:searchBar.text];
    // 放弃作为第一个响应者，关闭键盘
    [searchBar resignFirstResponder];
    
    JSearchProductController *searchVC = [[JSearchProductController alloc]init];
    searchVC.searchText = searchBar.text;
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    if (searchBar.text.length == 0) {
        searchBar.returnKeyType = UIReturnKeyDone;
    }
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    if (searchBar.text.length == 0) {
        searchBar.returnKeyType = UIReturnKeyDone;
    }
}

//- (void) filterBySubstring:(NSString*) subStr
//{
//    NSLog(@"----filterBySubstring------");
//    // 设置为搜索状态
////    isSearch = YES;
//    // 定义搜索谓词
//    NSPredicate* pred = [NSPredicate predicateWithFormat:
//                         @"SELF CONTAINS[c] %@" , subStr];
//    // 使用谓词过滤NSArray
////    searchData = [tableData filteredArrayUsingPredicate:pred];
//    // 让表格控件重新加载数据
////    [tableView reloadData];
//}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
